﻿using System;
using System.Configuration;
using System.Data;
using System.Text;
using Common;
using HCRS.DBACL;
using HCRS.Errors;
using HCRS.Implementation;

namespace JinJiang.BookingEngine.Web.cn
{
    public class SearchHotelClass
    {
        private CommonWeb _CommonClass = new CommonWeb();
        private DataAccessLayer _DataAccessLayer = new DataAccessLayer();
        private Functions _Functions = new Functions();
        private HotelClass _HotelClass = new HotelClass();

        private string[] mapKey = new[]
                                      {
                                          "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
                                      };

        public static int PageCount;
        public string photourl = ConfigurationManager.AppSettings["photourl"];
        public string ratecode = ConfigurationManager.AppSettings["ratecode"];
        public static int TotalRecord;

        private string getAmenity(string amenity, int lan)
        {
            var sbamenity = new StringBuilder();
            if (lan == 0x1)
            {
                if ((amenity.IndexOf("* Business Center") > 0x0) || (amenity.IndexOf("* 商务中心") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/ABusiness.gif' hspace='2' title='Business Center'/>");
                }
                if (((amenity.IndexOf("* Dinner") > 0x0) || (amenity.IndexOf("* Restaurant") > 0x0)) || (amenity.IndexOf("* 餐厅") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/ARestaurant.gif' hspace='2' title='Dinner'/>");
                }
                if ((amenity.IndexOf("* Beauty Salon") > 0x0) || (amenity.IndexOf("* Spa水疗护理中心") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/ASpa.gif' hspace='2' title='Beauty Salon'/>");
                }
                if (((amenity.IndexOf("* Exercise Gym") > 0x0) || (amenity.IndexOf("* 健身中心") > 0x0)) || (amenity.IndexOf("* 配有运动场所") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/AFitness.gif' hspace='2' title=' Exercise Gym'/>");
                }
                if (((amenity.IndexOf("High Speed Internet") > 0x0) || (amenity.IndexOf("宽带上网") > 0x0)) || (amenity.IndexOf("因特网接口") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/AInternet.gif' hspace='2' title='High Speed Internet'/>");
                }
                if (amenity.IndexOf("* 室内游泳池") > 0x0)
                {
                    sbamenity.Append("<img src='../Images/hotel/APool.gif' hspace='2' title='室内游泳池'/>");
                }
            }
            else
            {
                if ((amenity.IndexOf("* Business Center") > 0x0) || (amenity.IndexOf("* 商务中心") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/ABusiness.gif' hspace='2' alt='商务中心'/>");
                }
                if (((amenity.IndexOf("* Dinner") > 0x0) || (amenity.IndexOf("* Restaurant") > 0x0)) || (amenity.IndexOf("* 餐厅") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/ARestaurant.gif' hspace='2' alt='餐厅'/>");
                }
                if ((amenity.IndexOf("* Beauty Salon") > 0x0) || (amenity.IndexOf("* Spa水疗护理中心") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/ASpa.gif' hspace='2' alt='Spa'/>");
                }
                if (((amenity.IndexOf("* Exercise Gym") > 0x0) || (amenity.IndexOf("* 健身中心") > 0x0)) || (amenity.IndexOf("* 配有运动场所") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/AFitness.gif' hspace='2' alt='健身中心'/>");
                }
                if (((amenity.IndexOf("* High Speed Internet") > 0x0) || (amenity.IndexOf("* 宽带上网") > 0x0)) || (amenity.IndexOf("* 因特网接口") > 0x0))
                {
                    sbamenity.Append("<img src='../Images/hotel/AInternet.gif' hspace='2' alt='宽带上网'/>");
                }
                if (amenity.IndexOf("* 室内游泳池") > 0x0)
                {
                    sbamenity.Append("<img src='../Images/hotel/APool.gif' hspace='2' alt='室内游泳池'/>");
                }
            }
            return sbamenity.ToString();
        }

        public string GetGuaImage(string gua)
        {
            var str = "";
            var guaCode = gua;
            if (guaCode == null)
            {
                return str;
            }
            if (guaCode != "PP" && guaCode != "DP")
            {
                if (guaCode != "CC")
                {
                    return str;
                }
            }
            else
            {
                return "<img src=\"images/money.gif\" width=\"11\" height=\"11\" title=\"本房型需预付保证\" />";
            }
            return "<img src=\"images/visa.gif\" width=\"11\" height=\"11\"/>";
        }

        public string GetHotels(string hotels, int gid)
        {
            string hotelsList;
            try
            {
                var dt = CommonDataAccess.GetTable("select H.*,D.Amenities from (select  pid as P, Hotel_ID, replace(Title,'\"','&quot;') as Title, pid,keywords, Stars, Diamond,replace(convert(nvarchar(4000),subtitle),'\"','&quot;') as subtitle, LowPrice, highprice, photourl, replace(Address1,'\"','&quot;') as Address1, citygbid, districtid, commercialid, localLongitude, localLatitude,GroupID,pagerank  from hotel where  Passed=1 and  pid in(select  pid  from hotel where groupid=" + gid + ")) H left join   H_description  D on H.pid=D.property_id and d.languagetype=0  order by pagerank desc", "hotel");
                var str = new StringBuilder(hotels + "={");
                var temp = new StringBuilder();
                for (var i = 0x0; i < dt.Rows.Count; i++)
                {
                    str.Append("P" + dt.Rows[i][0x0] + ":[");
                    for (var c = 0x1; c < (dt.Columns.Count - 0x1); c++)
                    {
                        str.Append("\"" + dt.Rows[i][c].ToString().Replace(Convert.ToChar(0x0).ToString(), "").Replace(Convert.ToChar(0xd).ToString(), "").Replace(Convert.ToChar(0xa).ToString(), "") + "\",");
                    }
                    str.Append("\"" + getAmenity(dt.Rows[i][dt.Columns.Count - 0x1].ToString(), 0x0).Replace("\n", "") + "\"],");
                }
                str.Append("P:[0,0]};");
                hotelsList = str.ToString().Replace(Convert.ToChar(0x0).ToString(), "");
            }
            catch (Exception e)
            {
                WebError.SendErrorEmail(e);
                throw e;
            }
            return hotelsList;
        }

        public DataTable GetHotelsByPID(string hotels, string PIDS, int lan)
        {
            var Address = "Address1";
            if (lan == 0x1)
            {
                Address = "Address1_e";
            }
            var dt = CommonDataAccess.GetTable("select Isnull(Title,replace(Title,'\"','&quot;')) as title,Isnull(Address1,replace(Address1,'\"','&quot;')) as address,h_city.name as city,pid,stars,hotel.photourl,hotel.citygbid from hotel left join h_city on hotel.citygbid=h_city.citygbid where pid in (" + PIDS + ") order by hotel.citygbid, stars desc  ", "hotel");
            if ((dt != null) && (dt.Rows.Count > 0x0))
            {
                return dt;
            }
            return null;
        }

        public string GetPrices(string array, int gid, string RateCode, string checkIN, string checkOUT)
        {
            var dt = CommonDataAccess.GetTable(string.Concat(new object[]
                                                                 {
                                                                     "select distinct  Property_id,PriceNoTax from h_cache where      avstat!='N' and priceNoTax>0  and (datediff(hh,getdate(),Availdate)+24>=cutoff)  and  Availdate>= '", checkIN, "' AND Availdate<'", checkOUT, "'    and  RateCode in(", RateCode, ")     and Property_id in(select  pid  from hweb_hotel where gid=", gid, ") order by Property_id,PriceNoTax "
                                                                 }), "price");
            var str = new StringBuilder(array + "=[");
            for (var i = 0x0; i < dt.Rows.Count; i++)
            {
                str.Append("[" + dt.Rows[i][0x0] + "," + dt.Rows[i][0x1] + "],");
            }
            str.Append("[0,0]");
            str.Append("];");
            return str.ToString();
        }

        private string getStar(string star)
        {
            var sbStar = new StringBuilder();
            sbStar.Append("<img src='");
            if (star == "0")
            {
                sbStar.Append("../Images/hotel/value.gif");
            }
            else
            {
                sbStar.Append("../Images/hotel/" + star + "star.gif");
            }
            sbStar.Append("'/>");
            return sbStar.ToString();
        }

        public string pagination(int total, int per, int page, string query_string)
        {
            var allpage = 0x0;
            var next = 0x0;
            var pre = 0x0;
            var startcount = 0x0;
            var endcount = 0x0;
            var pagestr = "<br/>";
            if (page < 0x1)
            {
                page = 0x1;
            }
            if (per != 0x0)
            {
                allpage = total / per;
                allpage = ((total % per) != 0x0) ? (allpage + 0x1) : allpage;
                allpage = (allpage == 0x0) ? 0x1 : allpage;
            }
            next = page + 0x1;
            pre = page - 0x1;
            startcount = ((page + 0x5) > allpage) ? (allpage - 0x9) : (page - 0x4);
            endcount = (page < 0x5) ? 0xa : (page + 0x5);
            object htmlString = (pagestr + "<table width=\"98%\"  border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\">") + "<tr>" + "<td width=\"50%\" height=\"30\">";
            pagestr = string.Concat(new[]
                                        {
                                            htmlString, "<div >第", page, "页，总", allpage, "页，点击页码查看下一页</strong></div>"
                                        }) + "<td width=\"50%\" valign=\"middle\">" + "<div align=\"right\">";
            if (startcount < 0x1)
            {
                startcount = 0x1;
            }
            if (allpage < endcount)
            {
                endcount = allpage;
            }
            pagestr = pagestr + ((page > 0x1) ? ("<a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(1)\" ><img src=\"../Images/hotel/nav_first.gif\" width=\"15\" height=\"10\" hspace=\"5\" border=\"0\"></a><a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(" + pre + ")\" ><img src=\"../Images/hotel/nav_left.gif\" hspace=\"5\" border=\"0\"></a>") : "<img src=\"../Images/hotel/nav_first1.gif\" width=\"15\" height=\"10\" hspace=\"5\"><img src=\"../Images/hotel/nav_left1.gif\" hspace=\"5\">");
            for (var i = startcount; i <= endcount; i++)
            {
                pagestr = pagestr + ((page == i) ? ("&nbsp;&nbsp;<font color=\"#ff0000\">" + i + "</font>") : string.Concat(new object[]
                                                                                                                                {
                                                                                                                                    "&nbsp;&nbsp;<a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(", i, ")\" >", i, "</a>"
                                                                                                                                }));
            }
            return ((pagestr + ((page != allpage) ? string.Concat(new object[]
                                                                      {
                                                                          "&nbsp;<a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(", next, ")\" ><img src=\"../Images/hotel/nav_right.gif\" hspace=\"5\" border=\"0\"></a><a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(", allpage, ")\" ><img src=\"../Images/hotel/nav_last.gif\" width=\"15\" height=\"10\" hspace=\"5\" border=\"0\"></a>"
                                                                      }) : "<img src=\"../Images/hotel/nav_right1.gif\" hspace=\"5\"><img src=\"../Images/hotel/nav_last1.gif\" width=\"15\" height=\"10\" hspace=\"5\" border=\"0\">") + "</div>") + "</td></tr>" + "</table>");
        }

        public bool RateCodeAvailable(int hotelID, string PorH, string rateCode, out string availableRateCode)
        {
            var allratecode = "";
            DataTable dt = null;
            if (PorH == "hid")
            {
                dt = CommonDataAccess.GetTable("select RateCode from CRM_hotelweb_domain where property_id=(select pid from hotel where hotel_id=" + hotelID.ToString() + ") and languagetype=0", "tab");
            }
            else
            {
                dt = CommonDataAccess.GetTable("select RateCode from CRM_hotelweb_domain where property_id=" + hotelID.ToString() + " and languagetype=0", "tab");
            }
            try
            {
                allratecode = dt.Rows[0x0]["RateCode"].ToString();
            }
            catch
            {
                allratecode = ratecode;
            }
            var TempRateClass = allratecode.Replace("|", ",");
            if (!string.IsNullOrEmpty(rateCode))
            {
                if (("," + TempRateClass + ",").IndexOf(rateCode + ",") >= 0x0)
                {
                    availableRateCode = rateCode;
                    return true;
                }
                availableRateCode = "";
                return false;
            }
            availableRateCode = allratecode;
            return true;
        }
    }
}